<template>
    <div class="login-wrap">
        <!-- left common -->
        <div class="login-banner"></div>
        <div class="login-right">
            <div class="login-content">
                <p class="login-tipe">上传授权文件</p>
                <p class="card-lic" v-if="code">机器码:{{ code }}</p>
                <p class="card-lic" v-else style="cursor: pointer;" @click="getCode">机器码:<i class="el-icon-refresh"></i>刷新</p>
                <p class="card-lic">将机器码交给相关人员获取许可文件</p>
                <el-row :gutter="10">
                    <el-upload v-if="!time" class="review-dts-upload" action="#" :auto-upload="false" :show-file-list="false"
                        accept=".lic, .LIC" :on-change="uploadChange" drag v-loading="fileLoading">
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    </el-upload>
                    <div v-else class="card-lic">许可到期时间：{{ time }}</div>
                </el-row>
                <div class="other-box">
                    <div class="password-register" @click.prevent="goHome()">返回首页</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import request from '@/js/util/url.js';
import PATH from '@/js/util/path.js';
import message from '@/js/util/message.js';
import bus from '@/js/event/bus.js';
import user from '@/js/util/user.js';

export default {
    data() {
        return {
            fileLoading: false,
            code: '',
            time: ''
        };
    },
    created() {
        this.getCode();
    },
    mounted() {},
    methods: {
        getCode() {
            let datas = request.getLicenseCode();
            if (datas.state.code === 10000) {
                this.code = datas.body.message;
            } else {
                this.$message.error(datas.body.message);
                this.code = '';
            }
        },
        uploadChange(uploadFile, uploadFiles) {
            this.fileLoading = true;
            let formData = new FormData();
            formData.set('file', uploadFile.raw);
            let datas = request.uploadLic(formData);
            if (datas.state.code === 10000) {
                this.$message.success('上传成功。');
                this.time = datas.body.message;
                this.goHome();
            } else {
                this.time = '';
                this.$message.error(datas.body.message);
            }
            this.fileLoading = false;
        },
        goHome() {
            let datas = request.getLicenseQuery();
            if (datas.state.code === 10000) {
                user.setToken('dtscharts');
                user.setUploadToken('dtscharts_upload');
                if (user.getPanel() === 'true') {
                    if (this.$route.query && this.$route.query.id) {
                        bus.initExporePlayer({ id: this.$route.query.id, layout: this.$route.query.layout ? this.$route.query.layout : '1' });
                    } else {
                        bus.initExporePlayer();
                    }
                } else {
                    this.login();
                }
            } else if (datas.state.code === 10004) {
                this.$router.push(PATH.LOGINLIC);
            } else {
                this.$message.error(datas.body.message);
            }
        },
        login() {
            this.$router.push(PATH.SCENE);
        }
    }
};
</script>

<style lang='scss' scoped>
@import url('@/css/login/login.scss');
.card-lic {
    font-size: 14px;
    font-weight: bold;
    user-select: text;
    margin-bottom: 5px;
    color: white;
}
</style>